import cryptoJs from 'crypto-js';
export var baseUrl = location.host.includes('xnh.xnhkfpt.com')?'//xnh.xnhkfpt.com':'//xnhloc.xnhkfpt.com/qudao'
export var apiBaseUrl = 'http://xnhloc.xnhkfpt.com'
export var baseUrl2 = location.host.includes('xnh.xnhkfpt.com')?'//xnh.xnhkfpt.com':'//xnhloc.xnhkfpt.com'
export var phoneReg = /^1[3456789]\d{9}$/
export var emailReg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/
export var idcardReg = /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/
export var telephoneReg = /^((\+?86)|(\(\+86\)))?(13[012356789][0-9]{8}|15[012356789][0-9]{8}|18[02356789][0-9]{8}|147[0-9]{8}|1349[0-9]{7})$/
export var DESKey = 'sinon!@#_vue'
console.log(cryptoJs)
//DES加密
export const encryptDes = (message) => {
	var keyHex = cryptoJs.enc.Utf8.parse(DESKey)
	var option = {
		mode: cryptoJs.mode.ECB,
		padding: cryptoJs.pad.Pkcs7
	}
	var encrypted = cryptoJs.DES.encrypt(message, keyHex, option)
	return encrypted.ciphertext.toString().toUpperCase()
} 

export function getURLQueryString(name) {
	var url = decodeURIComponent(window.location.href);
	url = url.replace(/&quot/g, "\"");
	var r;
	if(url.indexOf("#?") > 0) {
		url = url.replace("#?", "&");
		r = url.match(new RegExp("(^|&)" + name + "=([^&]*)(&|$)"));
	} else {
		r = window.location.search.substr(1).match(new RegExp("(^|&)" + name + "=([^&]*)(&|$)"))
	}
	return r ? r[2] : null;
}

export function setCookie(name, value, day) {
	var exp = new Date();
	exp.setTime(exp.getTime() + day * 24 * 60 * 60 * 1000);
	document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
}

//获取cookie
export function getCookie(objName) { //获取指定名称的cookie的值 
	var arrStr = document.cookie.split("; ");
	for(var i = 0; i < arrStr.length; i++) {
		var temp = arrStr[i].split("=");
		if(temp[0] == objName) {
			return decodeURI(temp[1]);
		}
	}
}
//excel流数据导出excel
export function exportExcel (data, name) {
 let blob = new Blob([data], { type: "application/vnd.ms-excel" }); // 设置文件类型excel
 let url = window.URL.createObjectURL(blob); // 创建一个临时的url指向blob对象
 // 创建url之后可以模拟对此文件对象的一系列操作，例如：预览、下载
 let a = document.createElement("a");
 a.href = url;
 a.download = name + ".xlsx";
 a.click();
 // 释放这个临时的对象url
 window.URL.revokeObjectURL(url);
}

function isClass(o) {
	if(o === null) return "Null";
	if(o === undefined) return "Undefined";
	return Object.prototype.toString.call(o).slice(8, -1);
}

//深度克隆
export function deepClone(obj) {
	if(!obj) {
		return null
	}
	var result, oClass = isClass(obj);
	//确定result的类型
	if(oClass === "Object") {
		result = {};
	} else if(oClass === "Array") {
		result = [];
	} else {
		return obj;
	}

	for(var key in obj) {
		if(obj.hasOwnProperty(key)) {
			var copy = obj[key];
			if(isClass(copy) == "Object") {
				result[key] = deepClone(copy); //递归调用
			} else if(isClass(copy) == "Array") {
				result[key] = deepClone(copy);
			} else {
				result[key] = obj[key];
			}
		}
	}
	return result;
}

Date.prototype.Format = function(fmt) { //author: meizz 
	var o = {
		"M+": this.getMonth() + 1, //月份 
		"d+": this.getDate(), //日 
		"h+": this.getHours(), //小时 
		"m+": this.getMinutes(), //分 
		"s+": this.getSeconds(), //秒 
		"q+": Math.floor((this.getMonth() + 3) / 3), //季度 
		"S": this.getMilliseconds() //毫秒 
	};
	if(/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
	for(var k in o)
		if(new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
	return fmt;
}

export function getBrowserType(){
    var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
    var browser='unknown';
    if (userAgent.indexOf("IE")!=-1) {
        browser="IE";
    }else if(userAgent.indexOf('Firefox')!=-1){
        browser="Firefox";
    }else if(userAgent.indexOf('OPR')!=-1){
        browser="Opera";
    }else if(userAgent.indexOf('Chrome')!=-1){
        browser="Chrome";
    }else if(userAgent.indexOf('Safari')!=-1){
        browser="Safari";
    }else if(userAgent.indexOf('Trident')!=-1){
        browser='IE 11';
    }
    return browser;
}